#include "window_music.h"
#include "window_manager.h"

lv_obj_t *play_img=NULL; //播放背景图片对象
lv_obj_t *play_btn=NULL; //播放暂停按钮
lv_obj_t *down_btn=NULL; //上一曲按钮
lv_obj_t *up_btn  =NULL; //下一曲按钮


static bool play_flag=true;  //播放标志
static bool up_flag  =true;  //上一曲标志
static bool down_flag=true;  //下一曲标志



lv_obj_t *play_song_name  =NULL; //播放歌曲的名字
lv_obj_t *play_song_author=NULL; //播放歌曲的作者

lv_anim_t a;   //动画效果




static void set_angle(void * img, int32_t v)
{
  
	lv_img_set_angle(img, v);
	//printf("%s   value=%d\r\n",__FUNCTION__,v);
}

static void my_event_cb(lv_event_t * event)
{
	//判断当前的对象
	lv_obj_t * obj = lv_event_get_target(event);
	
	//lv_obj_t* label = (lv_obj_t* )lv_event_get_user_data(event);  获取当前数据
	
  if(obj==play_btn)
	{
		
		if(play_flag)
	{
		play_flag=false;
		lv_img_set_src(play_btn, &music_pause1); //图片抬起
		
		
		
		
		//lv_img_set_pivot(play_img, 50, 50);    /*Rotate around the top left corner*/
	  lv_anim_init(&a);
    lv_anim_set_var(&a, play_img);
    lv_anim_set_exec_cb(&a, set_angle);
    lv_anim_set_values(&a, 0, 3600);
    lv_anim_set_time(&a, 5000);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
		
    lv_anim_start(&a);
		
		
		 printf("pause Clicked\n");
	}
	else
	{		
		//删除动画
		lv_anim_del(play_img,NULL);
		
		
		play_flag=true;
		lv_img_set_src(play_btn, &music_start1); //图片按下
		 printf("play Clicked\n");
	}
	

	}else if(obj==down_btn)
	{
		if(!down_flag)
	{
		lv_img_set_src(play_btn, &music_pause1); //图片抬起
	}
	else
	{		
		lv_img_set_src(play_btn, &music_start1); //图片按下
	}
	 printf("play Clicked\n");

	
	}else if(obj==up_btn)
	{

		if(!up_flag)
	{
		lv_img_set_src(play_btn, &music_pause1); //图片抬起
	}
	else
	{		
		lv_img_set_src(play_btn, &music_start1); //图片按下
	}
	 printf("Up Clicked\n");


	}


	

}



static void slider_event_callback(lv_event_t* event)
{
    lv_obj_t * slider = lv_event_get_target(event);
    if (slider != NULL)
    {
        lv_obj_t* label = (lv_obj_t* )lv_event_get_user_data(event);
        if (label != NULL)
        {
            // 刷新label对象的文本
            lv_label_set_text_fmt(label, "音量%d", lv_slider_get_value(slider));
            lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15);  // 设置对齐，label对象在slider对象上方中间位置
        }
    }
}


void window_music_init()
{
    //播放
    play_btn = lv_imgbtn_create(window_manger[WINDOW_MUSIC].window_obj);
	  lv_obj_set_size(play_btn, music_start1.header.w, music_start1.header.h);
    lv_obj_set_pos(play_btn,240/2 -32,280/2 -32 +90);       //设置坐标
    lv_img_set_src(play_btn, &music_start1);//设置背景图片
	  lv_obj_add_event_cb(play_btn, my_event_cb, LV_EVENT_CLICKED, NULL);//增加按键处理回调函数

	  //上一曲
    up_btn= lv_imgbtn_create(window_manger[WINDOW_MUSIC].window_obj);
	  lv_obj_set_size(up_btn, music_previous1.header.w, music_previous1.header.h);
    lv_obj_set_pos(up_btn,240/2 -20 - 100,280/2 -51/2 +90);       //设置坐标
    lv_img_set_src(up_btn, &music_previous1);//设置背景图片
	  lv_obj_add_event_cb(up_btn, my_event_cb, LV_EVENT_CLICKED, NULL);//增加按键处理回调函数
	  //下一曲
	  down_btn= lv_imgbtn_create(window_manger[WINDOW_MUSIC].window_obj);
	  lv_obj_set_size(down_btn, music_next1.header.w, music_next1.header.h);
    lv_obj_set_pos(down_btn,240/2 -32 +100,280/2 -51/2 +90);       //设置坐标
    lv_img_set_src(down_btn, &music_next1);//设置背景图片
	  lv_obj_add_event_cb(down_btn, my_event_cb, LV_EVENT_CLICKED, NULL);//增加按键处理回调函数
	
	  //创建音量滑动条
	#if 0	
		lv_obj_t * slider = lv_slider_create(window_manger[WINDOW_MUSIC].window_obj); // 创建滑块对象
    if (slider != NULL)
    {
        lv_obj_set_width(slider, 200); // 设置slider的宽度		
		lv_obj_set_width(slider,200);  //设置宽度
		lv_obj_set_pos(slider,(360 -200)/2,300);
		//lv_obj_align_to(window_manger[WINDOW_MUSIC].window_obj, slider, LV_ALIGN_OUT_TOP_MID, 0, 20);  // 标签对象放在slider对象的上方中间位
        //lv_obj_center(slider);         	 //对样显示在屏幕中央
        lv_slider_set_range(slider, 0, 100); // 设置滑块值的变化范围10-200
        #if 0
        lv_obj_t* label = lv_label_create(window_manger[WINDOW_MUSIC].window_obj); // 创建一个标签，用于显示滑块的滑动值
        if (label != NULL)
        {
            lv_label_set_text_fmt(label, "%d", lv_slider_get_min_value(slider)); // 标签默认显示滑块的最小值
            lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15);  // 标签对象放在slider对象的上方中间位置
            // 添加滑块值变化事件和事件回调函数，并将label对象最为事件的user_data
            lv_obj_add_event_cb(slider, slider_event_callback, LV_EVENT_VALUE_CHANGED, (void *)label);
        }
				#endif

	}
	#endif
	
		//音乐播放背景
    play_img= lv_img_create(window_manger[WINDOW_MUSIC].window_obj);
	  lv_obj_set_size(play_img, play_bg.header.w, play_bg.header.h);
    lv_obj_set_pos(play_img,10,50);       //设置坐标
    lv_img_set_src(play_img, &play_bg);//设置背景图片
	  //lv_obj_add_event_cb(play_img, my_event_cb, LV_EVENT_CLICKED, NULL);//增加按键处理回调函数

	
	
	
	
	//音乐字符
	lv_obj_t*  logo = lv_label_create(window_manger[WINDOW_MUSIC].window_obj); 
	lv_label_set_recolor(logo, true);   /*Enable re-coloring by commands in the text*/
	lv_obj_set_style_text_font(logo, &language_36, 0);
	if(current_language==LANGUAGE_TYPE_CHINESE)
	{
		lv_label_set_text_fmt(logo,"#ffffff 音乐#");
	}else 
	if(current_language==LANGUAGE_TYPE_ENGLISH)
	{
		lv_label_set_text_fmt(logo,"#ffffff Music#");
	}
	
	lv_obj_align(logo, LV_ALIGN_TOP_MID,0,10);
	
	
	
	//显示音乐名称
	play_song_name = lv_label_create(window_manger[WINDOW_MUSIC].window_obj); 
	lv_label_set_recolor(play_song_name, true);   /*Enable re-coloring by commands in the text*/

	lv_label_set_text_fmt(play_song_name,"#ffffff Sorry#");
	lv_obj_set_style_text_font(play_song_name, &language_36, 0);
	//lv_obj_set_pos(play_song_name,200,300);
	lv_obj_align(play_song_name, LV_ALIGN_TOP_MID,0,160);
	
	//显示音乐作者
	play_song_author = lv_label_create(window_manger[WINDOW_MUSIC].window_obj); 
	lv_label_set_recolor(play_song_author, true);   /*Enable re-coloring by commands in the text*/
	lv_label_set_text_fmt(play_song_author,"#ffffff Justin#");
	lv_obj_set_style_text_font(play_song_author, &language_36, 0);
	lv_obj_set_pos(play_song_author,110,100);
	
}
